package youfu

import (
	"encoding/json"
	"fmt"
	"math/rand"
	"strconv"
	"time"
)

func getReqId() string {
	nowe := time.Now()
	myString := fmt.Sprintf("%s%d", strconv.FormatInt(nowe.Unix(), 10), rand.Intn(10000))
	return myString
}

// 商户账户余额查询
func (cli *Client) QueryMerIdBalance(req *QueryMerIdBalanceReq) (*QueryMerIdBalanceResp, error) {
	cli.reqId = getReqId()

	commReqMsg, err := cli.buildCommReq(jsonMarshal(req), FunCode_QueryMerIdBalance)
	if err != nil {
		return nil, fmt.Errorf("buildCommReq failed. err: %s", err.Error())
	}
	fmt.Printf("commReqMsg: %s\n", jsonMarshal(commReqMsg))

	resp, err := cli.Post(commReqMsg)
	if err != nil {
		return nil, fmt.Errorf("Post failed. err: %s", err.Error())
	}

	commRespMessage, err := cli.parseCommResp(resp)
	if err != nil {
		return nil, fmt.Errorf("parseCommResp failed. err: %s", err.Error())
	}
	fmt.Printf("【HTTP响应结果处理】结束: %s\n", jsonMarshal(commRespMessage))

	apiResp := &QueryMerIdBalanceResp{}
	err = json.Unmarshal([]byte(commRespMessage.ResData), &apiResp)
	if err != nil {
		return nil, fmt.Errorf("json.Unmarshal failed. err: %s", err.Error())
	}

	return apiResp, nil
}

// 商户批量付款
func (cli *Client) BatchTransfer(req *BatchTransferReq) (*BatchTransferResp, error) {
	cli.reqId = getReqId()

	commReqMsg, err := cli.buildCommReq(jsonMarshal(req), FunCode_BatchTransfer)
	if err != nil {
		return nil, fmt.Errorf("buildCommReq failed. err: %s", err.Error())
	}
	fmt.Printf("commReqMsg: %s\n", jsonMarshal(commReqMsg))

	resp, err := cli.Post(commReqMsg)
	if err != nil {
		return nil, fmt.Errorf("Post failed. err: %s", err.Error())
	}
	fmt.Printf("resp: %s\n", string(resp))

	commRespMessage, err := cli.parseCommResp(resp)
	if err != nil {
		return nil, fmt.Errorf("parseCommResp failed. err: %s", err.Error())
	}
	fmt.Printf("commRespMsg: %s\n", jsonMarshal(commRespMessage))

	apiResp := &BatchTransferResp{}
	err = json.Unmarshal([]byte(commRespMessage.ResData), &apiResp)
	if err != nil {
		return nil, fmt.Errorf("json.Unmarshal failed. err: %s", err.Error())
	}

	return apiResp, nil
}

// 商户批量付款查询
func (cli *Client) QueryBatchTransferResult(req *QueryBatchTransferResultReq) (*QueryBatchTransferResultResp, error) {
	cli.reqId = getReqId()

	commReqMsg, err := cli.buildCommReq(jsonMarshal(req), FunCode_QueryBatchTranferResult)
	if err != nil {
		return nil, fmt.Errorf("buildCommReq failed. err: %s", err.Error())
	}
	fmt.Printf("commReqMsg: %s\n", jsonMarshal(commReqMsg))

	resp, err := cli.Post(commReqMsg)
	if err != nil {
		return nil, fmt.Errorf("Post failed. err: %s", err.Error())
	}
	fmt.Printf("resp: %s\n", string(resp))

	commRespMessage, err := cli.parseCommResp(resp)
	if err != nil {
		return nil, fmt.Errorf("parseCommResp failed. err: %s", err.Error())
	}
	fmt.Printf("commRespMsg: %s\n", jsonMarshal(commRespMessage))

	apiResp := &QueryBatchTransferResultResp{}
	err = json.Unmarshal([]byte(commRespMessage.ResData), &apiResp)
	if err != nil {
		return nil, fmt.Errorf("json.Unmarshal failed. err: %s", err.Error())
	}

	return apiResp, nil
}

// 自由职业者签约
func (cli *Client) ProfessionalSign(req *ProfessionalSignReq) (*ProfessionalSignResp, error) {
	cli.reqId = getReqId()

	commReqMsg, err := cli.buildCommReq(jsonMarshal(req), FunCode_ProfessionalSign)
	if err != nil {
		return nil, fmt.Errorf("buildCommReq failed. err: %s", err.Error())
	}
	fmt.Printf("commReqMsg: %s\n", jsonMarshal(commReqMsg))

	resp, err := cli.Post(commReqMsg)
	if err != nil {
		return nil, fmt.Errorf("Post failed. err: %s", err.Error())
	}
	fmt.Printf("resp: %s\n", string(resp))

	commRespMessage, err := cli.parseCommResp(resp)
	if err != nil {
		return nil, fmt.Errorf("parseCommResp failed. err: %s", err.Error())
	}
	fmt.Printf("commRespMsg: %s\n", jsonMarshal(commRespMessage))

	apiResp := &ProfessionalSignResp{}
	err = json.Unmarshal([]byte(commRespMessage.ResData), &apiResp)
	if err != nil {
		return nil, fmt.Errorf("json.Unmarshal failed. err: %s", err.Error())
	}

	return apiResp, nil
}

// 自由职业者签约查询
func (cli *Client) QueryProfessionalSignResult(req *QueryProfessionalSignResultReq) (*QueryProfessionalSignResultResp, error) {
	cli.reqId = getReqId()

	commReqMsg, err := cli.buildCommReq(jsonMarshal(req), FunCode_QueryProfessionalSignResult)
	if err != nil {
		return nil, fmt.Errorf("buildCommReq failed. err: %s", err.Error())
	}
	fmt.Printf("commReqMsg: %s\n", jsonMarshal(commReqMsg))

	resp, err := cli.Post(commReqMsg)
	if err != nil {
		return nil, fmt.Errorf("Post failed. err: %s", err.Error())
	}
	fmt.Printf("resp: %s\n", string(resp))

	commRespMessage, err := cli.parseCommResp(resp)
	if err != nil {
		return nil, fmt.Errorf("parseCommResp failed. err: %s", err.Error())
	}
	fmt.Printf("commRespMsg: %s\n", jsonMarshal(commRespMessage))

	apiResp := &QueryProfessionalSignResultResp{}
	err = json.Unmarshal([]byte(commRespMessage.ResData), &apiResp)
	if err != nil {
		return nil, fmt.Errorf("json.Unmarshal failed. err: %s", err.Error())
	}

	return apiResp, nil
}

// 自由职业者解约
func (cli *Client) ProfessionalUnSign(req *ProfessionalUnSignReq) (*ProfessionalUnSignResp, error) {
	cli.reqId = getReqId()

	commReqMsg, err := cli.buildCommReq(jsonMarshal(req), FunCode_QueryProfessionalSignResult)
	if err != nil {
		return nil, fmt.Errorf("buildCommReq failed. err: %s", err.Error())
	}
	fmt.Printf("commReqMsg: %s\n", jsonMarshal(commReqMsg))

	resp, err := cli.Post(commReqMsg)
	if err != nil {
		return nil, fmt.Errorf("Post failed. err: %s", err.Error())
	}
	fmt.Printf("resp: %s\n", string(resp))

	commRespMessage, err := cli.parseCommResp(resp)
	if err != nil {
		return nil, fmt.Errorf("parseCommResp failed. err: %s", err.Error())
	}
	fmt.Printf("commRespMsg: %s\n", jsonMarshal(commRespMessage))

	apiResp := &ProfessionalUnSignResp{}
	err = json.Unmarshal([]byte(commRespMessage.ResData), &apiResp)
	if err != nil {
		return nil, fmt.Errorf("json.Unmarshal failed. err: %s", err.Error())
	}

	return apiResp, nil
}
